import fs from 'fs';
import path from 'path';
import isElectron from 'is-electron';
import electron from 'electron';

class PathManager {
    constructor() {
        console.log('-------------- pathManager --------------');

        this.tempDirPath = null; // 存放临时文件
        // exe 文件路径
        this.potracePath = null;
        this.mkbitmapPath = null;

        if (isElectron()) {
            const app = electron.app || electron.remote.app;
            // appPath 在 dev 阶段不使用；electron 打包后才使用对应：${app-name}.app/Contents/Resources/app
            const appPath = app.getAppPath();
            this.tempDirPath = app.getPath('temp');
            switch (process.platform) {
                case 'darwin':
                    this.potracePath = path.join(
                        appPath,
                        'static',
                        'potrace-1.16.mac-x86_64',
                        'potrace'
                    );
                    this.mkbitmapPath = path.join(
                        appPath,
                        'static',
                        'potrace-1.16.mac-x86_64',
                        'mkbitmap'
                    );
                    break;
                case 'win32':
                    this.potracePath = path.join(
                        appPath,
                        'static',
                        'potrace-1.16.win64',
                        'potrace.exe'
                    );
                    this.mkbitmapPath = path.join(
                        appPath,
                        'static',
                        'potrace-1.16.win64',
                        'mkbitmap.exe'
                    );
                    break;
            }
        } else {
            this.tempDirPath = './tempDir';
            !fs.existsSync(this.tempDirPath) && fs.mkdirSync(this.tempDirPath);
            switch (process.platform) {
                case 'darwin':
                    this.potracePath = './static/potrace-1.16.mac-x86_64/potrace';
                    this.mkbitmapPath = './static/potrace-1.16.mac-x86_64/mkbitmap';
                    break;
                case 'win32':
                    this.potracePath = './static/potrace-1.16.win64/potrace.exe';
                    this.mkbitmapPath = './static/potrace-1.16.win64/mkbitmap.exe';
                    break;
            }
        }

        console.log('isElectron: ' + isElectron());
        if (isElectron()) {
            console.log('appPath: ' + (electron.app || electron.remote.app).getAppPath());
        }
        console.log('tempPath: ' + this.tempDirPath);
        console.log('potracePath: ' + this.potracePath);
        console.log('mkbitmapPath: ' + this.mkbitmapPath);

        // exe 文件必须全存在
        if (!fs.existsSync(this.potracePath)) {
            console.error('potrace.exe not exist', this.potracePath);
        }
        if (!fs.existsSync(this.mkbitmapPath)) {
            console.error('mkbitmap.exe not exist', this.mkbitmapPath);
        }
        console.log('-----------------------------------------');
    }
}

const pathManager = new PathManager();

export default pathManager;
